﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FHJFPro.Utils.Helper
{
    public static class JsonHelper
    {
        private static object o = new object();
        //public static string ToJson(object obj)
        //{
        //    if (obj == null)
        //    {
        //        return "";
        //    }
        //    return JsonConvert.SerializeObject(obj);
        //}

        public static string ToJson(this object obj)
        {
            if (obj == null)
            {
                return "";
            }
            return JsonConvert.SerializeObject(obj);
        }

        public static string ToJson(this object obj, JsonSerializerSettings settings)
        {
            if (obj == null)
            {
                return "";
            }
            if (settings == null)
            {
                settings = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };
            }
            return JsonConvert.SerializeObject(obj, settings);
        }

        public static string ToJsonFormat(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            return JsonConvert.SerializeObject(obj, Formatting.Indented);
        }

        //public static T FromJson<T>(string json)
        //{
        //    return JsonConvert.DeserializeObject<T>(json);
        //}
        public static T FromJson<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        public static object FromJson(string json, Type t)
        {
            return JsonConvert.DeserializeObject(json, t);
        }
        public static List<T> FromJsonList<T>(string value)
        {
            return JsonConvert.DeserializeObject<List<T>>(value);
        }

        public static T FromFile<T>(string filename)
        {
            lock (o)
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                {
                    StreamReader sr = new StreamReader(fs);
                    var content = sr.ReadToEnd();
                    sr.Close();
                    return content.FromJson<T>();
                }
            }
        }

        public static void ToFile(object obj, string filename)
        {
            lock (o)
            {
                FileInfo fi = new FileInfo(filename);
                if (!Directory.Exists(fi.DirectoryName))
                {
                    Directory.CreateDirectory(fi.DirectoryName);
                }
                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    StreamWriter sr = new StreamWriter(fs);
                    sr.Write(ToJson(obj));
                    sr.Close();
                }
            }
        }

        public static void ToFile(object obj, string filename, Encoding encoding)
        {
            lock (o)
            {
                FileInfo fi = new FileInfo(filename);
                if (!Directory.Exists(fi.DirectoryName))
                {
                    Directory.CreateDirectory(fi.DirectoryName);
                }
                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    StreamWriter sr = new StreamWriter(fs, encoding);
                    sr.Write(ToJsonFormat(obj));
                    sr.Close();
                }
            }
        }
    }
}
